package gr.atc.epart;

import gr.atc.epart.googlewrapper.moderator.GoogleModerator;
import gr.atc.epart.googlewrapper.moderator.ModeratorSubmission;
import gr.atc.epart.googlewrapper.moderator.ModeratorTopic;
import gr.atc.epart.googlewrapper.moderator.ModeratorVote;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class DeliberationSubmissionAdapter extends BaseAdapter {

	private Context _context;
	private ArrayList<ModeratorSubmission> _submisions;
	private Handler _messageHandler = new Handler();
	

	public DeliberationSubmissionAdapter(Context context,
			ArrayList<ModeratorSubmission> submisions) {
		_context = context;
		_submisions = submisions;
		
	}

	@Override
	public int getCount() {
		return _submisions.size();
	}

	@Override
	public Object getItem(int i) {
		return _submisions.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewgroup) {
		// all submissions are retrieved at once..no need to implement
		// ask-on-demand
		final ModeratorSubmission submission = _submisions.get(i);

		
		View v = view;
		final int position = i;

		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.topicsubmissionrow, null);
		}

		if (submission != null) {
			
			TextView submissionText = (TextView) v
					.findViewById(R.id.SubmissionText);
			TextView submissionAttribution = (TextView) v
					.findViewById(R.id.SubmissionAttribution);

			TextView positiveVoteText = (TextView) v
					.findViewById(R.id.positiveVotes);
			TextView negativeVoteText = (TextView) v
					.findViewById(R.id.negativeVotes);

			try {

				positiveVoteText.setText(String.valueOf(submission.getCounter(ModeratorSubmission.PLUS_VOTES)));
				negativeVoteText.setText(String.valueOf(submission.getCounter(ModeratorSubmission.MINUS_VOTES)));
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			submissionText.setText(submission.getText());

			SimpleDateFormat formatter = new SimpleDateFormat(
					"EEE, MMM d, yyyy");

			submissionAttribution.setText(String.format("%s,%s posted %s",
					submission.getAttribution().getName(), submission
							.getAttribution().getLocation(), formatter
							.format(submission.getCreationDate())));

			Button agreeButton = (Button) v
					.findViewById(R.id.submissionagreebutton);
			Button disagreeButton = (Button) v
					.findViewById(R.id.submissiondisagreebutton);

			OnClickListener agreeButtonListener = new OnClickListener() {
				public void onClick(View view) {

					
					Toast.makeText(_context, "Submitting vote...",Toast.LENGTH_LONG).show();

					GoogleModerator _moderator = EPartVolatileDataStore
							.getInstance().getModeratorInstance();

					ModeratorVote vote = new ModeratorVote(
							ModeratorVote.Vote.Plus, ModeratorVote.VoteFlag.Ok);
					_moderator.voteSubmission(submission, vote);
									
				}
			};

			OnClickListener disagreeButtonListener = new OnClickListener() {
				public void onClick(View v) {
					GoogleModerator _moderator = EPartVolatileDataStore
							.getInstance().getModeratorInstance();

					Toast.makeText(_context, "Submitting vote...",Toast.LENGTH_LONG).show();

					ModeratorVote vote = new ModeratorVote(
							ModeratorVote.Vote.Minus, ModeratorVote.VoteFlag.Ok);
					_moderator.voteSubmission(submission, vote);
					
					

				}
			};

			agreeButton.setOnClickListener(agreeButtonListener);
			disagreeButton.setOnClickListener(disagreeButtonListener);
		}

		return v;
	}

	public ArrayList<ModeratorSubmission> getSubmisions() {
		return _submisions;
	}

	public void setSubmisions(ArrayList<ModeratorSubmission> submisions) {
		_submisions = submisions;
		this.notifyDataSetChanged();
	}

}
